package com.ruoyi.ExpertReport.controller;

import com.ruoyi.ExpertReport.service.ReportService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.sun.deploy.net.URLEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;
import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@RestController
@RequestMapping("/expert")
public class ExpertReportController {
    @Autowired
    private ReportService reportService;

    @PostMapping("/downLoad")
    public void downloadReport(HttpServletResponse response) {
        try {
            byte[] reportData = reportService.generateReport(SecurityUtils.getUserId());

            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            response.setHeader("Content-Disposition", "attachment; filename=ExpertReport.docx");
            response.setHeader("Access-Control-Allow-Origin", "*");

            // 写入输出流
            ServletOutputStream out = response.getOutputStream();
            out.write(reportData);
            out.flush();
            out.close();
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }


}
